package com.acme.bankapp.domine.bank;

import com.acme.bankapp.domine.bank.ClientRegistrationListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Bank {
    private int countClients;
    private static final int MAX_COUNT_CLIENTS = 10;
    private Client[] clients;
    private List<ClientRegistrationListener> listeners;

    public Bank(final List<ClientRegistrationListener> listeners) {
        clients = new Client[MAX_COUNT_CLIENTS];
        this.listeners = listeners;
        addListeners(new DebugListener());
    }

    public static class PrintClientListener
        implements ClientRegistrationListener {
        @Override
        public void onClientAdded(final Client client) {
            System.out.println(client.getClientSalutation());
        }
    }

    public static class EmailNotificationListener
        implements ClientRegistrationListener {
        @Override
        public void onClientAdded(final Client client) {
            System.out.println("Notification email for client "
            + client.getClientSalutation() + " to be sent");
        }
    }

    public static class DebugListener implements ClientRegistrationListener {
        @Override
        public void onClientAdded(final Client client) {
            System.out.println(client.getClientSalutation() + " " + new Date());
        }
    }

    public void addListeners(final ClientRegistrationListener
            clientRegistrationListener) {
        listeners.add(clientRegistrationListener);
    }

    public int getCountClients() {
        return countClients;
    }

    public Bank() {
        clients = new Client[MAX_COUNT_CLIENTS];
        listeners = new ArrayList<ClientRegistrationListener>();
        addListeners(new DebugListener());
    }

    public Client[] getClients() {
        return clients;
    }

    public void addClient(final Client client) {
        if (countClients < MAX_COUNT_CLIENTS) {
            clients[countClients++] = client;
            for (ClientRegistrationListener clientRegistrationListener
                    :listeners) {
                clientRegistrationListener.onClientAdded(client);
            }
        } else {
            System.out.println("Storage of clients filled completely");
        }
    }
}
